<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">楼栋名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入楼栋名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="buildingInfoBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                            <button id="buildingInfoBtnExport" class="layui-btn icon-btn color-green">导出</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="buildingInfoTable" lay-filter="buildingInfoTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="buildingInfoTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="floor">楼层管理</a>
    <a class="layui-btn layui-btn-xs" lay-event="room">房号管理</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="buildingInfoForm">
    <form lay-filter="buildingInfoForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">所属企业</label>
                <div class="layui-input-block">
                    <select name="companyId" id="companyId"></select>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">所属厂区</label>
                <div class="layui-input-block">
                    <select name="factoryId" id="factoryId"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">楼栋编号</label>
                <div class="layui-input-block">
                    <input name="code" placeholder="请输入楼栋编号" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">楼栋名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入楼栋名称" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">空间性质</label>
                <div class="layui-input-block">
                    <select name="spaceCharacterId" id="spaceCharacterId"></select>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">建筑面积</label>
                <div class="layui-input-block">
                    <input name="coveredArea" placeholder="请输入建筑面积（/平方）" type="number" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">使用面积</label>
                <div class="layui-input-block">
                    <input name="useArea" placeholder="请输入使用面积（/平方）" type="number" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">单位月费用</label>
                <div class="layui-input-block">
                    <input name="monthPrice" placeholder="请输入单位月费用(/元)" type="number" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">楼栋性质</label>
                <div class="layui-input-block">
                    <input type="radio" name="buildingCharacter" value="0" title="自建"/>
                    <input type="radio" name="buildingCharacter" value="1" title="租用" checked/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">图纸附件</label>
                <div class="layui-input-block">
                    <div id="btnUpload14" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传图片</div>
                    <div id="btnUpload14_path" style="margin-left: 260px;line-height: 40px;"></div>
                    <input id="accessoryImg" name="accessoryImg" type="hidden" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">序号</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入序号" type="text" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="buildingInfoFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- 楼层管理 -->
<script type="text/html" id="floorInfoWindow">
    <div class="layui-fluid" style="padding: 0 5px;">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button id="floorInfoBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
<!--                            <button id="floorInfoBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="floorInfoTable" lay-filter="floorInfoTable"></table>
            </div>
        </div>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="floorInfoTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="floorInfoForm">
    <form lay-filter="floorInfoForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">所属楼栋</label>
                <div class="layui-input-block">
                    <select name="buildingId" id="buildingId"></select>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">楼层编码</label>
                <div class="layui-input-block">
                    <input name="code" placeholder="请输入楼层编码" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">楼层名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入楼层名称" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">空间性质</label>
                <div class="layui-input-block">
                    <select name="spaceCharacterId" id="spaceCharacterId"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item" >
            <div class="layui-col-md6">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">序号</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入序号" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="floorInfoFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>


<!-- 房号管理 -->
<script type="text/html" id="roomInfoWindow">
    <div class="layui-fluid" style="padding: 0 5px;">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <button id="roomInfoBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
<!--                            <button id="roomInfoBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>-->
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="roomInfoTable" lay-filter="roomInfoTable"></table>
            </div>
        </div>
    </div>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="roomInfoForm">
    <form lay-filter="roomInfoForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">所属楼栋</label>
                <div class="layui-input-block">
                    <select name="buildingId" id="buildingId"></select>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">所属楼层</label>
                <div class="layui-input-block">
                    <select name="floorId" id="floorId"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">房号</label>
                <div class="layui-input-block">
                    <input name="roomNumber" placeholder="请输入房号" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">房间名称</label>
                <div class="layui-input-block">
                    <input name="roomName" placeholder="请输入房间名称" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">空间性质</label>
                <div class="layui-input-block">
                    <select name="spaceCharacterId" id="spaceCharacterId"></select>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">房间面积</label>
                <div class="layui-input-block">
                    <input name="roomArea" placeholder="请输入房间面积" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">房间类型</label>
                <div class="layui-input-block">
                    <select name="roomTypeId" id="roomTypeId"></select>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">房间状态</label>
                <div class="layui-input-block">
                    <select name="roomState" id="roomState">
                        <option value="0">闲置</option>
                        <option value="1">部分闲置</option>
                        <option value="2">满员</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md6">
                <label class="layui-form-label">房间图片</label>
                <div class="layui-input-block">
                    <div id="btnUpload14" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传图片</div>
                    <div id="btnUpload14_path" style="margin-left: 260px;line-height: 40px;"></div>
                    <input name="roomImg" type="hidden" class="layui-input" lay-verify=""
                           id="roomImg"/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">已入住人数</label>
                <div class="layui-input-block">
                    <input name="enterNumber" placeholder="请输入已入住人数" type="number" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item" >
            <div class="layui-col-md6">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input name="deleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
                </div>
            </div>
            <div class="layui-col-md6">
                <label class="layui-form-label">序号</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入序号" type="text" class="layui-input" lay-verify="required" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入描述"  class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="roomInfoFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>


<!-- 表格操作列 -->
<script type="text/html" id="roomInfoTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#buildingInfoTable',
            url: config.base + 'enterprise/system/admin/buildingInfo/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'sortNumber', title: '序号', fixed: 'left'}
                ,{field: 'code', title: '楼栋编号', fixed: 'left'}
                ,{field: 'name',  title: '楼栋名称'}
                ,{field: 'companyName',  title: '所属企业'}
                ,{field: 'factoryName',  title: '所属厂区'}
                ,{field: 'remark',  title: '描述'}
                ,{field: 'spaceCharacterName',  title: '空间性质'}
                ,{field: 'coveredArea', title: '建筑面积'}
                ,{field: 'useArea',  title: '使用率'}
                ,{field: 'deleted', sort: true, title: '是否启用',templet:function (d) {
                        if(d.deleted){
                            return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.deleted+'" lay-filter="deleted"  lay-text="启用|停用"/>';
                        }else{
                            return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="deleted"   lay-text="启用|停用"/>';
                        }
                    }}
                ,{field: 'createTime',  title: '创建时间'}
                ,{align: 'center', toolbar: '#buildingInfoTableBar', title: '操作', width: 300, fixed: 'right'}
            ]],
        });

        // 工具条点击事件
        table.on('tool(buildingInfoTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }  else if (obj.event === 'floor') { // 删除
                showFloorModel(data);
            }else if (obj.event === 'room') { // 删除
                showRoomModel(data);
            }
        });

        //监听排序
        table.on('sort(buildingInfoTable)', function(obj){
            table.reload('buildingInfoTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#buildingInfoBtnAdd').click(function () {
            showEditModel();
        });
        //显示楼层管理
        function showFloorModel(data) {
            admin.open({
                type: 1,
                area: '1100px',
                offset: '65px',
                title: '楼层管理',
                content: $('#floorInfoWindow').html(),
                success: function () {
                    // 渲染表格
                    var insTb = table.render({
                        elem: '#floorInfoTable',
                        url: config.base + 'enterprise/system/admin/floorInfo/list?buildingId='+data.id,
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            ,{field: 'sortNumber', sort: true, title: '序号'}
                            ,{field: 'buildingName', sort: true, title: '所属楼栋'}
                            ,{field: 'code', sort: true, title: '楼层编码'}
                            ,{field: 'name', sort: true, title: '楼层名称'}
                            ,{field: 'spaceCharacterName', sort: true, title: '空间性质'}
                            ,{field: 'deleted', sort: true, title: '是否启用',templet:function (d) {
                                    if(d.deleted){
                                        return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.deleted+'" lay-filter="deleted"  lay-text="启用|停用"/>';
                                    }else{
                                        return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="deleted"   lay-text="启用|停用"/>';
                                    }
                                }}
                            ,{field: 'createTime', sort: true, title: '创建时间'}
                            ,{align: 'center', toolbar: '#floorInfoTableBar', title: '操作', width: 200}
                        ]],
                        done: function (res, curr, count) {

                        }
                    });

                    // 工具条点击事件
                    table.on('tool(floorInfoTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') { //修改
                            showFloorEditModel(data);
                        } else if (obj.event === 'del') { // 删除
                            doDelete(obj);
                        }
                    });

                    //监听排序
                    table.on('sort(floorInfoTable)', function(obj){
                        table.reload('floorInfoTable', {
                            initSort: obj
                            ,where: {
                                sort: obj.field
                                ,order: obj.type
                            }
                        });
                    });

                    // 添加按钮点击事件
                    $('#floorInfoBtnAdd').click(function () {
                        showFloorEditModel();
                    });


                    form.on('switch(deleted)',function(obj){
                        if(obj.elem.checked){
                            enable(obj.value)
                        }else{
                            disable(obj.value)
                        }
                    })

                    function enable(id){
                        admin.req('enterprise/system/admin/floorInfo/enable', {id:id}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload('systemWorkmanshipTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                    }

                    function disable(id){
                        admin.req('enterprise/system/admin/floorInfo/disable', {id:id}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload('systemWorkmanshipTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                    }

                    // 显示编辑弹窗
                    function showFloorEditModel(data) {

                        admin.req('enterprise/system/admin/spaceCharacter/list', {limit:9999}, function (res) {

                        }, 'GET');

                        admin.req('enterprise/system/admin/buildingInfo/list', {limit:9999}, function (res) {
                            if (res.code == 200) {
                                if (res.data.length > 0){
                                    $.each(res.data,function (index,item) {
                                        $("#buildingId").append(new Option(item.name,item.id));
                                    })
                                    form.render('select');
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                        admin.open({
                            type: 1,
                            area: '700px',
                            offset: '65px',
                            title: data ? '修改楼层' : '添加楼层',
                            content: $('#floorInfoForm').html(),
                            success: function () {
                                form.val('floorInfoForm', data);
                                // 表单提交事件
                                form.on('submit(floorInfoFormSubmit)', function (d) {
                                    layer.load(2);
                                    admin.req(data ? 'enterprise/system/admin/floorInfo/update' : 'enterprise/system/admin/floorInfo/add', d.field, function (res) {
                                        layer.closeAll('loading');
                                        if (res.code == 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            table.reload('floorInfoTable');
                                            layer.closeAll('page');
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }, data ? 'PUT' : 'POST');
                                    return false;
                                });
                            }
                        });
                    }if (res.code == 200) {
                        if (res.data.length > 0){
                            $.each(res.data,function (index,item) {
                                $("#spaceCharacterId").append(new Option(item.name,item.id));
                            })
                            form.render('select');
                        }
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }


                }
            })
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.req('user/admin/companyInfo/list', {limit:9999}, function (res) {
                if (res.code == 200) {
                    if (res.data.length > 0){
                        $.each(res.data,function (index,item) {
                            $("#companyId").append(new Option(item.name,item.id));
                        })
                        form.render('select');
                    }
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'GET');
            admin.req('enterprise/system/admin/factoryInfo/list', {limit:9999}, function (res) {
                if (res.code == 200) {
                    if (res.data.length > 0){
                        $.each(res.data,function (index,item) {
                            $("#factoryId").append(new Option(item.name,item.id));
                        })
                        form.render('select');
                    }
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'GET');

            admin.req('enterprise/system/admin/spaceCharacter/list', {limit:9999}, function (res) {
                if (res.code == 200) {
                    if (res.data.length > 0){
                        $.each(res.data,function (index,item) {
                            $("#spaceCharacterId").append(new Option(item.name,item.id));
                        })
                        form.render('select');
                    }
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'GET');
            admin.open({
                type: 1,
                area: '900px',
                offset: '65px',
                title: data ? '修改楼栋' : '添加楼栋',
                content: $('#buildingInfoForm').html(),
                success: function () {

                    //查询企业信息
                    var enterpriseInfo;
                    admin.req('enterprise/system/admin/enterpricceId/getEnterpricceIdVO', {}, function (res) {
                        enterpriseInfo = res.data;
                    }, 'get', false);
// 上传图片
                    upload.render({
                        elem: '#btnUpload14'
                        , url: config.upload_url + 'uploadFile'
                        , data: {"path": enterpriseInfo.enterpriseCode + "/"}
                        , accept: 'file'
                        , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                        , headers: {Authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                var imagUrl = res.data.url;
                                // if(imagUrl){
                                //     imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                // }
                                $('#btnUpload14_path').html('<img src="' + imagUrl + '" style="height: 50px;" />');
                                $('#accessoryImg').val(imagUrl);
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    form.val('buildingInfoForm', data);
                    // 表单提交事件
                    form.on('submit(buildingInfoFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/system/admin/buildingInfo/update' : 'enterprise/system/admin/buildingInfo/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('buildingInfoTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }
        //房间性质管理
        function showRoomModel(data){
            admin.open({
                type: 1,
                area: '1200px',
                offset: '65px',
                title: "房间性质管理",
                content: $('#roomInfoWindow').html(),
                success: function () {
                    // 渲染表格
                    var insTb = table.render({
                        elem: '#roomInfoTable',
                        url: config.base + 'enterprise/system/admin/roomInfo/list?buildingId='+data.id,
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            ,{field: 'buildingName', sort: true, title: '所属楼栋'}
                            ,{field: 'floorName', sort: true, title: '所属楼层'}
                            ,{field: 'roomNumber', sort: true, title: '房号'}
                            ,{field: 'spaceCharacterName', sort: true, title: '空间性质'}
                            ,{field: 'roomArea', sort: true, title: '房间面积'}
                            ,{field: 'roomTypeName', sort: true, title: '房间类型'}
                            ,{field: 'roomState', sort: true, title: '房间状态'}
                            ,{field: 'roomImg', sort: true, title: '房间图片'}
                            ,{field: 'enterNumber', sort: true, title: '已入住人数'}
                            ,{field: 'createTime', sort: true, title: '创建时间'}
                            ,{field: 'deleted', sort: true, title: '是否启用',templet:function (d) {
                                    if(d.deleted){
                                        return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.deleted+'" lay-filter="deleted"  lay-text="启用|停用"/>';
                                    }else{
                                        return '<input name="deleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="deleted"   lay-text="启用|停用"/>';
                                    }
                                }}
                            ,{align: 'center', fixed: 'right', toolbar: '#roomInfoTableBar', title: '操作', width: 200}
                        ]],
                        done: function (res, curr, count) {

                        }
                    });
                    // 添加按钮点击事件
                    $('#roomInfoBtnAdd').click(function () {
                        showRoomEditModel();
                    });
                    // 工具条点击事件
                    table.on('tool(roomInfoTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') { //修改
                            showRoomEditModel(data);
                        } else if (obj.event === 'del') { // 删除
                            roomDelete(obj);
                        }
                    });

                    // 删除
                    function roomDelete(obj) {
                        layer.confirm('确定要删除吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('enterprise/system/admin/roomInfo/' + obj.data.id, {}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    obj.del();
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'DELETE');
                        });
                    }
                    function showRoomEditModel(data) {
                        admin.open({
                            type: 1,
                            area: '820px',
                            offset: '65px',
                            title: data ? '修改房号管理' : '添加房号管理',
                            content: $('#roomInfoForm').html(),
                            success: function () {
                                if (data && data.roomImg) {
                                    $('#btnUpload14_path').html('<img src="' + data.roomImg + '" style="height: 50px;" />');
                                }
                                //查询企业信息
                                var enterpriseInfo;
                                admin.req('enterprise/system/admin/enterpricceId/getEnterpricceIdVO', {}, function (res) {
                                    enterpriseInfo = res.data;
                                }, 'get', false);
                                if ("" == enterpriseInfo || null == enterpriseInfo) {
                                    layer.msg("企业信息获取异常", {icon: 1});
                                    return;
                                }
                                //var productCode = $("input[name='productCode']").val();

                                upload.render({
                                    elem: '#btnUpload14'
                                    , url: config.upload_url + 'uploadFile'
                                    , data: {"path": enterpriseInfo.enterpriseCode + "/"}
                                    , accept: 'file'
                                    , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                                    , headers: {Authorization: 'Bearer ' + config.getToken()}
                                    , done: function (res) {
                                        if (res.code == 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            var imagUrl = res.data.url;
                                            // if(imagUrl){
                                            //     imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                            // }
                                            $('#btnUpload14_path').html('<img src="' + imagUrl + '" style="height: 50px;" />');
                                            $('#roomImg').val(imagUrl);
                                        } else if (res.code == 502) {
                                            return location.replace('admin/login.html');
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }
                                });

                                admin.req('enterprise/system/admin/roomType/list', {limit:9999}, function (res) {
                                    if (res.code == 200) {
                                        if (res.data.length > 0){
                                            $.each(res.data,function (index,item) {
                                                $("#roomTypeId").append(new Option(item.name,item.id));
                                            })
                                            form.render('select');
                                        }
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');

                                admin.req('enterprise/system/admin/floorInfo/list', {limit:9999}, function (res) {
                                    if (res.code == 200) {
                                        if (res.data.length > 0){
                                            $.each(res.data,function (index,item) {
                                                $("#floorId").append(new Option(item.name,item.id));
                                            })
                                            form.render('select');
                                        }
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');

                                admin.req('enterprise/system/admin/spaceCharacter/list', {limit:9999}, function (res) {
                                    if (res.code == 200) {
                                        if (res.data.length > 0){
                                            $.each(res.data,function (index,item) {
                                                $("#spaceCharacterId").append(new Option(item.name,item.id));
                                            })
                                            form.render('select');
                                        }
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');

                                admin.req('enterprise/system/admin/buildingInfo/list', {limit:9999}, function (res) {
                                    if (res.code == 200) {
                                        if (res.data.length > 0){
                                            $.each(res.data,function (index,item) {
                                                $("#buildingId").append(new Option(item.name,item.id));
                                            })
                                            form.render('select');
                                        }
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'GET');
                                form.val('roomInfoForm', data);
                                // 表单提交事件
                                form.on('submit(roomInfoFormSubmit)', function (d) {
                                    layer.load(2);
                                    admin.req(data ? 'enterprise/system/admin/roomInfo/update' : 'enterprise/system/admin/roomInfo/add', d.field, function (res) {
                                        layer.closeAll('loading');
                                        if (res.code == 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            table.reload('roomInfoTable');
                                            layer.closeAll('page');
                                        } else {
                                            layer.msg(res.msg, {icon: 2});
                                        }
                                    }, data ? 'PUT' : 'POST');
                                    return false;
                                });
                            }
                        });
                    }
                }
            });
        }
        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/buildingInfo/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {

                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });

        }
        form.on('switch(deleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })

        function enable(id){
            admin.req('enterprise/system/admin/buildingInfo/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('systemWorkmanshipTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id){
            admin.req('enterprise/system/admin/buildingInfo/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('systemWorkmanshipTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }
        // 导出excel
        $('#buildingInfoBtnExport').click(function () {
            var checkRows = table.checkStatus('buildingInfoTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>